package com.yzt.minemodule.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;

import com.yzt.zhuju.IService.message.IMessageService;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.arouter.RouterMain;
import com.yzt.zhuju.arouter.RouterMine;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.config.AppConfigCacheManager;
import com.yzt.zhuju.download.FileCacheManager;
import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.main.ExitAppEvent;
import com.yzt.zhuju.network.entity.mine.set.SettingEntry;
import com.yzt.zhuju.utils.AppDialogUtil;
import com.yzt.zhuju.utils.ViewUtil;
import com.yzt.minemodule.R;
import com.yzt.minemodule.adapter.SettingAdapter;
import com.yzt.p_base.utils.FileUtils;
import com.yzt.p_base.utils.ToastUtil;
import com.yzt.plugin_imageloader.ImageLoadUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 设置模块
 */
@Route(path = RouterMine.ACTIVITY_SETTING)
public class SettingActivity extends BaseActivity {

    @Autowired
    IMessageService messageService;

    private SettingAdapter settingAdapter;

    public static void start(Activity activity) {
        activity.startActivity(new Intent(activity, SettingActivity.class));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void installViews() {
        mTitleBar.setTitle("设置");
        RecyclerView recycle = findViewById(R.id.setting_rv);
        settingAdapter = new SettingAdapter();
        recycle.setLayoutManager(new LinearLayoutManager(mActivity));
        recycle.setAdapter(settingAdapter);

        List<SettingEntry> list = new ArrayList<>();
        list.add(new SettingEntry(2, "隐私设置"));
        list.add(new SettingEntry(3, "用户设置"));
        list.add(new SettingEntry(7, "清空缓存", fileSize()));
        list.add(new SettingEntry(10, "关于我们").setMsgTips(AppConfigCacheManager.getInstance().isNewVersion()));
        settingAdapter.addData(list);

        settingAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (ViewUtil.isFastDoubleClick()) {
                return;
            }
            switch (list.get(position).getType()) {
                case 5:
                    //黑名单
                    BlackListActivity.start(mActivity);
                    break;
                case 7:
                    //清除缓存空间
                    AppDialogUtil.showNoTitleDialog(mActivity, mActivity.getString(R.string.setting_cache_label), mActivity.getString(R.string.cancel),
                            mActivity.getString(R.string.sure), new AppDialogUtil.DialogClickInterface() {
                                @Override
                                public void onCancelClick() {

                                }

                                @Override
                                public void onSureClick() {
                                    ImageLoadUtil.clearImageAllCache(mActivity);
                                    FileCacheManager.getInstance().deleteCacheFile();
                                    ToastUtil.showMessage(R.string.str_clear_catch);
                                    settingAdapter.setData(getPosition(), new SettingEntry(7, mActivity.getString(R.string.setting_clear_catch), "0M"));
                                }
                            });
                    break;
                case 10:
                    //关于我们
                    AboutUsActivity.start(mActivity);
                    break;
                case 2:
                    RouterMain.goToWeb(getString(R.string.privacy_policy_url));
                    break;
                case 3:
                    RouterMain.goToWeb(getString(R.string.user_agreement_url));
                    break;
                default:
                    break;
            }
        });
        findViewById(R.id.tv_logout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //退出当前账号
                AppDialogUtil.showTitleDialog(mActivity, mActivity.getString(R.string.setting_exit_label), getString(R.string.setting_exit_desc), getString(R.string.sure),
                        mActivity.getString(R.string.cancel), new AppDialogUtil.DialogClickInterface() {
                            @Override
                            public void onCancelClick() {
                                messageService.logout();
                                RxBus.getDefault().post(new ExitAppEvent());
                                RXingUtils.logout();
                                finish();
                            }

                            @Override
                            public void onSureClick() {

                            }
                        });
            }
        });
    }

    private int getPosition() {
        List<SettingEntry> list = settingAdapter.getData();
        for (int i = 0, len = list.size(); i < len; i++) {
            SettingEntry settingEntry = list.get(i);
            if (settingEntry.getType() == 7) {
                return i;
            }
        }
        return 7;
    }

    public String fileSize() {
        long cacheSizeByte = ImageLoadUtil.getCacheSizeByte(mActivity);
        return FileUtils.getFormatSize(FileCacheManager.getInstance().getFileSize() + cacheSizeByte);
    }
}
